package com.teamwork.shareplatform.controller;
import com.teamwork.shareplatform.entity.FormPacket;
import com.teamwork.shareplatform.entity.User;
import com.teamwork.shareplatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.github.pagehelper.PageInfo;

// 表示这是一个 RESTful 风格的控制器
@RestController
// 定义该控制器下所有接口的基础路径
@RequestMapping("/user")
public class UserController {
    

    // 自动注入 UserService 实例
    @Autowired
    private UserService userService;

    // 查询所有用户的接口
    @RequestMapping("/findAll")
    public List<User> findAll() {
        return userService.findAll();
    }

    // 用户登录接口
    @PostMapping("/login")
    public CommonResult<User> login(@RequestParam("telephone") String telephone,
                                    @RequestParam("password") String password,
                                    @RequestParam("utype") int utype) {
        User user = userService.checkUser(telephone, password, utype);
        CommonResult<User> commonResult = new CommonResult<>();
        if (user != null) {
            commonResult.setCode(200);
            commonResult.setMessage("登录成功");
            commonResult.setData(user);
        } else {
            commonResult.setCode(-1);
            commonResult.setMessage("用户名或者密码错误");
        }
        return commonResult;
    }

    // 用户注册接口
    @PostMapping(value = "/register")
    public CommonResult<User> userRegister(@RequestBody User user) {
        int uid = userService.addUser(user);
        CommonResult<User> commonResult = new CommonResult<>();
        if (uid > 0) {
            User newuser = userService.queryById(uid);
            commonResult.setCode(200);
            commonResult.setMessage("注册成功");
            commonResult.setData(newuser);
        } else {
            commonResult.setCode(-1);
            commonResult.setMessage("注册失败");
        }
        return commonResult;
    }

    // 获取用户信息列表接口
    @GetMapping(value = "/list")
    public CommonResult<PageInfo> userList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        List<User> users = userService.queryAllUser(pageNum, pageSize);
        PageInfo pageInfo = new PageInfo(users);
        CommonResult<PageInfo> commonResult = new CommonResult<>();
        commonResult.setCode(200);
        commonResult.setMessage("获取成功");
        commonResult.setData(pageInfo);
        return commonResult;
    }

    // 获取单个用户信息接口
    @GetMapping(value = "/getOne")
    public CommonResult<User> getOneUser(@RequestParam("id") int id) {
        User user = userService.queryById(id);
        CommonResult<User> commonResult = new CommonResult<>();
        if (user != null && user.getId() > 0) {
            commonResult.setCode(200);
            commonResult.setMessage("获取成功");
            commonResult.setData(user);
        } else {
            commonResult.setCode(-1);
            commonResult.setMessage("没有该用户");
        }
        return commonResult;
    }

    // 添加或更新用户接口
    @PostMapping(value = "/doEdit")
    public CommonResult<Integer> addOrUpdateUser(@RequestBody FormPacket formPacket) {
        CommonResult<Integer> commonResult = new CommonResult<>();
        int flag;
        if ("add".equals(formPacket.getOperate())) {
            flag = userService.addUserByEdit(formPacket.getUserinfo());
            if (flag > 0) {
                commonResult.setCode(200);
                commonResult.setMessage("添加成功");
                commonResult.setData(flag);
            } else {
                commonResult.setCode(-1);
                commonResult.setMessage("添加失败");
            }
        } else if ("update".equals(formPacket.getOperate())) {
            flag = userService.updateUser(formPacket.getUserinfo());
            if (flag > 0) {
                commonResult.setCode(200);
                commonResult.setMessage("修改成功");
                commonResult.setData(flag);
            } else {
                commonResult.setCode(-1);
                commonResult.setMessage("修改失败");
            }
        } else {
            commonResult.setCode(-1);
            commonResult.setMessage("操作类型错误");
        }
        return commonResult;
    }

    // 删除用户接口
    
    @GetMapping(value = "/doDel")
    public CommonResult<Integer> delUser(@RequestParam("id") int id) {
        CommonResult<Integer> commonResult = new CommonResult<>();
        int flag = userService.deleteUser(id);
        if (flag > 0) {
            commonResult.setCode(200);
            commonResult.setMessage("删除成功");
            commonResult.setData(flag);
        } else {
            commonResult.setCode(-1);
            commonResult.setMessage("删除失败");
        }
        return commonResult;
    }


}